<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class mcategory extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "category";
    public $primaryKey = "cat_id";
    public $resCode = "CATEGORY";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
	$fieldData['cat_name'] = '';
	$fieldData['cat_code'] = '';
	$fieldData['cat_description'] = '';
	$fieldData['cat_parent'] = 0;
	$fieldData['cat_order'] = 0;
	$fieldData['cat_status'] = 0;
	$fieldData['seo_h1'] = '';
	$fieldData['seo_description'] = '';
	$fieldData['seo_title'] = '';
	$fieldData['seo_keyword'] = '';
	$fieldData['cat_icon'] = '';
	$fieldData['cat_time_update'] = time();
	$fieldData['cat_time_create'] = time();
	return $fieldData;
    }
	
	// public function get_parent($parent_id = 0) {
		// return $this->gets(array('cat_parent' => $parent_id));
	// }
	
	public function get_category_path($cat_id) {
		$cate = $this->get($cat_id, 'cat_id');
		if ($cate->cat_parent != 0){
			$cate->parent = $this->get_category_path($cate->cat_parent);
		}
		return $cate;
	}
	
	public function get_cat_tree(){
		$cate_tree= array();
		$cate_sub= array();
		$categoryParent = $this->gets(array('CAT_PARENT'=>0));
		$categorySub = $this->gets(array('CAT_PARENT > '=>0));
		if($categorySub){
			foreach($categorySub as $sub){
				$cate_sub[$sub->cat_parent][$sub->cat_id] = '- '.$sub->cat_name;
			}
		}
		if ($categoryParent) {
			for($i=0; $i<count($categoryParent); $i++) {
				$cate_tree[$categoryParent[$i]->cat_id] = $categoryParent[$i]->cat_name;
				if(isset($cate_sub[$categoryParent[$i]->cat_id])){
					foreach($cate_sub[$categoryParent[$i]->cat_id] as $k=>$v){
						$cate_tree[$k] = $v;
					}
				}
			}
		}
		return $cate_tree;
	}
	
	public function get_array_slug() {
		$cates = $this->gets();
		$cates = $this->key_value($cates, 'cat_code');
		return $cates;
	}
	
	public function get_cat_tree_array($published = false){ 
		$cate_sub= array();
		$this->db->order_by('cat_order asc');
		if ($published) 
			$this->db->where('cat_status > ', 0);
		$categoryParent = $this->gets(array('CAT_PARENT'=>0));
		$this->db->order_by('cat_order asc');
		if ($published) 
			$this->db->where('cat_status > ', 0);

		$categorySub = $this->gets(array('CAT_PARENT > '=>0));
		if ($categoryParent) {
			foreach ($categoryParent as $parent) {
				$cate_sub[$parent->cat_id]['parent'] = $parent;
			}
		}
		if($categorySub){
			foreach($categorySub as $sub){
				$cate_sub[$sub->cat_parent]['child'][$sub->cat_id] = $sub;
			}
		} 
		
		return $cate_sub;
	}
	
	
	function get_recursive(&$allmenu, $parent = 0) {
		$id_field = 'cat_id';
		$parent_field = 'cat_parent';
		//get current child 
		$menu = array();
		if ($allmenu && is_array($allmenu)) foreach ($allmenu as $k=>$v) {
			if ($v->{$parent_field} == $parent) {
				$menu[] = $v;
				unset($allmenu[$k]);
			}
		}
		if ($menu) foreach ($menu as &$item) {
			$temp_menu = $this->get_recursive($allmenu, $item->{$id_field});
			if ($temp_menu) {
				$item->child = $temp_menu;
			}
		}
		return $menu;
	}
	public function get_catgory_by_parent ($parent = 0, $published = 0){
		$this->db->order_by('cat_order asc');
		$this->db->where('cat_parent >=', $parent);
		$this->db->where('cat_status >=', $published);
		$result = $this->gets();
		
		return $result;
	}
	public function get_catgory_tree($parent = 0) {
		$this->db->order_by('cat_parent asc');
		$this->db->order_by('cat_order asc');
		$this->db->where('cat_status >', 0);
		$all_menu = $this->db->get($this->mytable) ->result();
		
		$result = $this->get_recursive($all_menu, $parent);
		return $result;
		// return $this->scache->script('model.mmenu.get_menu.parent.'.$parent,'$this->get_recursive('.$parent.');');
		
	}
	
	public function get_catgory_array($parent = 0) {
		$tree = $this->get_catgory_tree($parent);
		$array= array();
		if ($tree) foreach ($tree as $element) {
			tree_to_array($element, $array);
		}
		return $array;
	}

	public function is_parent_category($catid=0){
		if($catid!=0){
			$query = $this->db->where('cat_id',$catid)->where('cat_parent =',0)->get($this->model)->row();
		}
		return false;
	}

	
	public function convert_to_text_tree($array, $prefix = '') {
		if ($array) foreach ($array as &$result) {
			$result['cat_name'] = str_repeat($prefix, ($result['_level'])) . $result['cat_name'];
		}
		return $array;
	}
	
	public function get_parent( $parent = 0 ) {
		$this->db->order_by('cat_parent asc');
		$this->db->order_by('cat_order asc');
		$this->db->where('cat_parent', 0);
		return $this->db->get($this->mytable) ->result();
	}
	
	public function get_subcate($id){
	    $this->db->order_by('cat_id desc');
	    $this->db->where('cat_status', 1);
	    $this->db->where('cat_parent >', 0);
	    $this->db->where_in('cat_id', $id);
	}

	public function get_subcate_by_parent($parent_id){
		$this->db->where('cat_status',1);
		$this->db->where('cat_parent',$parent_id);
		return $this->db->get($this->mytable)->result();
	}

	public function get_categories_for_client()
    {
    	return $this->get_cat_tree_array(true);
    }
}

?>